package com.ts.api.module.api.convert.market.context;

import com.ts.api.common.constant.api.ApiProviderEnum;
import com.ts.api.exp.ChannelMarketEventMissException;
import com.ts.api.module.api.convert.core.annotation.ApiChannelMarketConvertType;
import com.ts.api.module.api.convert.market.convert.ChannelMarketConvert;
import com.ts.api.module.api.convert.market.handler.ChannelMarketConvertHandler;
import com.ts.api.module.channel.entity.po.ChannelPO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 *  API上游渠道贷超-转换上下文
 *  TODO 需要更改为贷超写法模式
 *
 *  @author Pan
 */
@Component
public class ApiChannelMarketConvertContext {

    @Autowired
    private ChannelMarketConvert channelMarketConvert;

    /**
     *  获取渠道上游产品上下文
     *
     *  @author Pan
     *  @param  channelPO  api客户端
     *  @return ApiPlatformConvertHandler
     */
    public ChannelMarketConvertHandler getChannelMarket(ChannelPO channelPO, ApiChannelMarketConvertType apiChannelMarketConvertType) {
        //  我方接入
        if (ApiProviderEnum.WE.hasType(channelPO.getProviderType())) {
            String channelUnicode = channelPO.getUniqueCode();
            ChannelMarketConvertHandler handler = channelMarketConvert.getHandler(channelUnicode, apiChannelMarketConvertType);
            if (handler == null) {
                throw new ChannelMarketEventMissException("上游对接缺失渠道:" + channelPO.getUniqueCode());
            }
            return handler;
        }

        //  默认对方接入
        return channelMarketConvert.getDefaultHandler(apiChannelMarketConvertType);
    }
}
